package cn.moukou.tongji.ihealth.patient.controller;

import cn.moukou.tongji.ihealth.core.bean.Resp;
import cn.moukou.tongji.ihealth.patient.dao.DoctorMapper;
import cn.moukou.tongji.ihealth.patient.dao.PatientMapper;
import cn.moukou.tongji.ihealth.patient.pojo.CaseRecord;
import cn.moukou.tongji.ihealth.patient.pojo.Doctor;
import cn.moukou.tongji.ihealth.patient.pojo.Patient;
import cn.moukou.tongji.ihealth.patient.pojo.PatientReserve;
import cn.moukou.tongji.ihealth.patient.service.*;
import cn.moukou.tongji.ihealth.patient.util.Md5Util;
import com.aliyuncs.utils.StringUtils;
import com.baomidou.mybatisplus.extension.api.R;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;


@RestController
@RequestMapping("/ihealth/patient")
public class ApiController {


    @Autowired
    RedissonClient redissonClient;

    @Autowired
    CaseRecordService caseRecordService;

    @Autowired
    DoctorService doctorService;

    @Autowired
    PatientService patientService;

    @Autowired
    PatientReserveService reserveService;

    @Autowired
    SmsService smsService;

    @Autowired
    StringRedisTemplate redisTemplate;

    @GetMapping("/ping")
    public Resp<String> ping() {
        return Resp.ok("pong");
    }

    @GetMapping("/user-info/get")
    public Resp<String> getUserInfo(@RequestParam("id") String id) {
        System.out.println(id);
        return Resp.ok(id);
    }

    @PostMapping("/sms/send")
    public Resp<String> sendSms(@RequestParam("phoneNumber") String phoneNumber) {
        String prefix = "auth:phone:";
        String code = redisTemplate.opsForValue().get(prefix + phoneNumber);
        if(!StringUtils.isEmpty(code)) {
            return Resp.fail("验证码未过期");
        }

        code = UUID.randomUUID().toString().substring(0,4);
        System.out.println(code);
        if(smsService.sendSms(phoneNumber, code)) {
            redisTemplate.opsForValue().set(prefix+phoneNumber, code, 300, TimeUnit.SECONDS);
            return Resp.ok("发送成功");
        } else {
            return Resp.fail("发送失败");
        }
    }

    @PostMapping("/sms/auth")
    public Resp<String> smsAuth(@RequestParam("phoneNumber") String phoneNumber, @RequestParam("code") String code) {
        String prefix = "auth:phone:";
        String redisCode = redisTemplate.opsForValue().get(prefix + phoneNumber);
        if(!StringUtils.isEmpty(redisCode)) {
            if(redisCode.equals(code)) {
                return Resp.ok("验证成功");
            } else {
                return Resp.fail("验证失败");
            }
        }
        return Resp.fail("验证失败");
    }

    @PostMapping("/user/add")
    public Resp<String> addPatient(
            @RequestParam("id") String id,
            @RequestParam("name") String name,
            @RequestParam("sex") String sex,
            @RequestParam("age") int age,
            @RequestParam("phoneNumber") String phoneNumber,
            @RequestParam("code") String code,
            @RequestParam("password") String password
    ) {
        String prefix = "auth:phone:";
        String redisCode = redisTemplate.opsForValue().get(prefix + phoneNumber);
        if(!StringUtils.isEmpty(redisCode)) {
            if(redisCode.equals(code)) {
                patientService.insertOne(id, name, age, sex, phoneNumber, Md5Util.getMD5String(password));
                return Resp.ok("注册成功");
            } else {
                return Resp.fail("短信验证码错误");
            }
        }
        return Resp.fail("短信验证码错误");
    }

    @PostMapping("/reserve/add")
    public Resp<String> addReserve(@RequestParam("id") String id,
                                   @RequestParam("name") String name,
                                   @RequestParam("age") int age,
                                   @RequestParam("sex") String sex,
                                   @RequestParam("phoneNumber") String phoneNumber,
                                   @RequestParam("desc") String desc,
                                   @RequestParam("date")Date date) {
        int num = reserveService.insertOne(id, name, age, sex, phoneNumber, desc, date);
        if(num>0) {
            return Resp.ok("预约成功，请关注预约信息");
        } else {
            if(num == -1) {
                return Resp.fail("您还有未失效的预约");
            } else {
                return Resp.fail("预约失败");
            }
        }
    }

    @GetMapping("/reserve/my/get")
    public Resp<List<PatientReserve>> getMyReserves(@RequestParam("id") String pid) {
        return Resp.ok(reserveService.getMyReserves(pid));
    }

    @GetMapping("/reserve/one/get")
    public Resp<Object> getOneReserve(@RequestParam("id") Long id) {
        Map<String, Object> res = new HashMap<>();
        PatientReserve reserve = reserveService.getOne(id);
        if(reserve == null) {
            return Resp.ok(null);
        }
        String did = reserve.getDid();
        if(StringUtils.isEmpty(did)) {
            res.put("reserve", reserve);
            res.put("doctor", null);
            return Resp.ok(res);
        } else {
            Doctor doctor = doctorService.getOneDoctor(did);
            res.put("reserve", reserve);
            res.put("doctor", doctor);
            return Resp.ok(res);
        }
    }

    @PostMapping("/reserve/delete")
    public Resp<String> deleteReserve(@RequestParam("id") Long id) {
        int num = reserveService.deleteOne(id);
        if(num > 0) {
            return Resp.ok("删除成功");
        } else {
            return Resp.fail("删除失败");
        }
    }

    @GetMapping("/case-record/my/get")
    public Resp<List<CaseRecord>> getCaseRecordsByPid(@RequestParam("pid") String pid) {
        return Resp.ok(caseRecordService.getCaseRecordsByPid(pid));
    }

    @GetMapping("/case-record/one/get")
    public Resp<Object> getOneCaseRecord(@RequestParam("id") Long id) {
        Map<String, Object> res = new HashMap<>();
        CaseRecord caseRecord = caseRecordService.getOne(id);
        String did = caseRecord.getDid();
        Doctor doctor = doctorService.getOneDoctor(did);
        res.put("caseRecord", caseRecord);
        res.put("doctor", doctor);
        return Resp.ok(res);
    }

    @GetMapping("/doctor/list/get")
    public Resp<List<Doctor>> getDoctorList() {
        return Resp.ok(doctorService.getDoctors());
    }

    @GetMapping("/doctor/one/get")
    public Resp<Doctor> getOneDoctor(@RequestParam("id") String id) {
        return Resp.ok(doctorService.getOneDoctor(id));
    }
}
